/**
 * 
 */
package com.gmail.blackdog1987.carcare.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.gmail.blackdog1987.carcare.BaseActivity;
import com.gmail.blackdog1987.carcare.R;
import com.gmail.blackdog1987.carcare.adapter.CarRentAdapter;
import com.gmail.blackdog1987.carcare.entity.TCarRent;
import com.gmail.blackdog1987.carcare.entity.base.DataPage;
import com.gmail.blackdog1987.carcare.network.request.builder.CarrentListRequestBuilder;
import com.gmail.blackdog1987.carcare.network.response.CarrentListResponse;
import com.gmail.blackdog1987.carcare.network.response.handler.CarrentListResponseHandler;
import com.gmail.blackdog1987.carcare.network.response.handler.IResponseHandler;
import com.gmail.blackdog1987.carcare.network.response.handler.OnResponseListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

public class CarRentListActivity extends BaseActivity implements OnResponseListener,
	OnClickListener, OnLastItemVisibleListener,
	OnRefreshListener2<ListView>, OnItemClickListener{
	private Context mContext;
	private Intent mIntent;
	private TextView mUseage;
	private TextView mPrice;
	private PullToRefreshListView mListView;
	private ListView mActualListView;
	private CarRentAdapter mAdapter;
	
	private CarrentListRequestBuilder mReq = new CarrentListRequestBuilder();
	private CarrentListResponseHandler mHandler = new CarrentListResponseHandler();
	private DataPage<TCarRent> page;
	
	private int type;
	private int order;

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_carrent_list);
		mContext = this;
		mIntent = new Intent(this, CarRentDetailActivity_.class);
		initViews();
		initData(savedInstanceState);
		reg();
	}

	public void initViews() {
		mUseage = (TextView) findViewById(R.id.carrent_useage);
		mPrice = (TextView) findViewById(R.id.carrent_price);
		mListView = (PullToRefreshListView) findViewById(R.id.carrent_list);
		mListView.setMode(Mode.BOTH);
		mActualListView = mListView.getRefreshableView();
	}

	public void reg() {
		findViewById(R.id.back).setOnClickListener(this);
		mActualListView.setOnItemClickListener(this);
		mHandler.setOnResponseListener(this);
		mUseage.setOnClickListener(this);
		mPrice.setOnClickListener(this);
		mListView.setOnRefreshListener(this);
	}

	public void initData(Bundle savedInstanceState) {
		mAdapter = new CarRentAdapter(mContext, null);
		mActualListView.setAdapter(mAdapter);
		type = getIntent().getIntExtra("type", -1);
		mReq.setOrder(1)
			.setPageindex(1)
			.setPagesize(20)
			.setType(type)
			.postJsonRequest(mContext, mHandler);
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.carrent_price:
			order = 1;
			mUseage.setTextColor(getResources().getColor(R.color.green));
			mPrice.setTextColor(getResources().getColor(R.color.white));
			mUseage.setBackgroundResource(R.drawable.bg_tx_right);
			mPrice.setBackgroundResource(R.drawable.bg_tx_left_pressed);
			mReq.setType(type)
				.setPageindex(1)
				.setOrder(order)
				.setPagesize(20)
				.postJsonRequest(mContext, mHandler);
			break;
		case R.id.carrent_useage:
			order = 2;
			mPrice.setTextColor(getResources().getColor(R.color.green));
			mUseage.setTextColor(getResources().getColor(R.color.white));
			mUseage.setBackgroundResource(R.drawable.bg_tx_right_pressed);
			mPrice.setBackgroundResource(R.drawable.bg_tx_left);
			mReq.setType(type)
				.setPageindex(1)
				.setOrder(order)
				.setPagesize(20)
				.postJsonRequest(mContext, mHandler);
		default:
			break;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		mIntent.putExtra("data", mAdapter.getItem(position-1));
		startActivity(mIntent);
	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
		mReq.setType(type)
			.setPageindex(1)
			.setOrder(order)
			.setPagesize(20)
			.postJsonRequest(mContext, mHandler);
	}

	@Override
	public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
		if(page==null){
			mReq.setType(type)
				.setPageindex(1)
				.setOrder(order)
				.setPagesize(20)
				.postJsonRequest(mContext, mHandler);
		}else if(page.currentPageNo >= page.lastPageNo){
			Toast.makeText(mContext, "没有了",Toast.LENGTH_LONG).show();
			mListView.onRefreshComplete();
		}else{
			mReq.setType(type)
				.setPageindex(page.nextPage)
				.setOrder(order)
				.setPagesize(page.pageSize)
				.postJsonRequest(mContext, mHandler);
		}
	}

	@Override
	public void onLastItemVisible() {
		
	}

	@Override
	public void onResponse(IResponseHandler classz) {
		// insert db
		page = ((CarrentListResponse)classz.getResponse()).datas;
		if(page.currentPageNo <= 1){
			mAdapter.setDatas(page.datas);
		}else{
			mAdapter.addDatas(page.datas);
			mAdapter.notifyDataSetChanged();
		}
		mListView.onRefreshComplete();
	}

	@Override
	public void onError(IResponseHandler classz) {
		Toast.makeText(mContext, "加载失败", Toast.LENGTH_LONG).show();
		mListView.onRefreshComplete();
	}
}
